document.addEventListener('DOMContentLoaded', function() {
    // 获取DOM元素
    const guessInput = document.getElementById('guess-input');
    const guessButton = document.getElementById('guess-button');
    const restartButton = document.getElementById('restart-button');
    const message = document.getElementById('message');
    const history = document.getElementById('history');
    const attemptsDisplay = document.getElementById('attempts');
    
    // 游戏变量
    let secretNumber;
    let attempts;
    let gameOver;
    let guessHistory = [];
    
    // 初始化游戏
    function initGame() {
        secretNumber = Math.floor(Math.random() * 100) + 1;
        attempts = 10;
        gameOver = false;
        guessHistory = [];
        
        // 重置UI
        message.textContent = '开始游戏吧！你有10次机会。';
        message.className = '';
        history.textContent = '';
        attemptsDisplay.textContent = attempts;
        guessInput.value = '';
        guessInput.disabled = false;
        guessButton.style.display = 'inline-block';
        restartButton.style.display = 'none';
        
        // 聚焦输入框
        guessInput.focus();
        
        console.log('游戏已初始化，秘密数字是：', secretNumber);
    }
    
    // 检查猜测
    function checkGuess() {
        // 验证输入
        if (guessInput.value === '' || gameOver) {
            return;
        }
        
        // 获取猜测值
        const guess = parseInt(guessInput.value);
        
        // 验证范围
        if (guess < 1 || guess > 100) {
            message.textContent = '请输入1到100之间的数字！';
            return;
        }
        
        // 减少尝试次数
        attempts--;
        attemptsDisplay.textContent = attempts;
        
        // 添加到历史记录
        guessHistory.push(guess);
        updateHistory();
        
        // 检查猜测
        if (guess === secretNumber) {
            // 猜对了
            gameWon();
        } else if (attempts === 0) {
            // 没有尝试次数了
            gameLost();
        } else {
            // 继续游戏
            const diff = Math.abs(guess - secretNumber);
            let hint;
            let className;
            
            if (diff <= 5) {
                hint = '非常热！';
                className = 'hot';
            } else if (diff <= 15) {
                hint = '热！';
                className = 'warm';
            } else if (diff <= 30) {
                hint = '温';
                className = 'warm';
            } else if (diff <= 50) {
                hint = '冷';
                className = 'cool';
            } else {
                hint = '非常冷';
                className = 'cold';
            }
            
            if (guess > secretNumber) {
                message.textContent = `${guess} 太大了！${hint}`;
            } else {
                message.textContent = `${guess} 太小了！${hint}`;
            }
            
            message.className = className;
            guessInput.value = '';
            guessInput.focus();
        }
    }
    
    // 更新历史记录
    function updateHistory() {
        if (guessHistory.length > 0) {
            history.textContent = `你猜过的数字: ${guessHistory.join(', ')}`;
        } else {
            history.textContent = '';
        }
    }
    
    // 游戏胜利
    function gameWon() {
        message.textContent = `恭喜你！你猜对了！秘密数字就是 ${secretNumber}！用了 ${10 - attempts} 次尝试。`;
        message.className = 'win';
        endGame();
    }
    
    // 游戏失败
    function gameLost() {
        message.textContent = `游戏结束！秘密数字是 ${secretNumber}。再试一次吧！`;
        message.className = 'lose';
        endGame();
    }
    
    // 结束游戏
    function endGame() {
        gameOver = true;
        guessInput.disabled = true;
        guessButton.style.display = 'none';
        restartButton.style.display = 'inline-block';
    }
    
    // 事件监听器
    guessButton.addEventListener('click', checkGuess);
    
    guessInput.addEventListener('keypress', function(e) {
        if (e.key === 'Enter') {
            checkGuess();
        }
    });
    
    restartButton.addEventListener('click', initGame);
    
    // 初始化游戏
    initGame();
});